import {defineStore} from 'pinia'
import {domainList} from '@api/domainApi'
import {useApi} from '@/axios/tool/useRequest'

export const useDomainStore = defineStore('domain-store', {
  state: () => {
    return {
      loading: false,
      domainList: [],
    }
  },
  getters: {},
  actions: {
    setDomainState() {
      this.$patch({
        loading: true,
        domainList: []
      })
    },
    async getDomainList(type, search = '') {
      this.domainList = []
      const {loading, result, request} = useApi(domainList({type, search}))
      this.loading = loading
      await request()
      result.value?.code === 0 ? this.domainList = result.value.data : ''
    },
    updateDomainList(data) {
      this.setDomainState()
      try {
        this.domainList = [...data]
      } finally {
        this.loading = false
      }
    }
  }
})
